import 'package:flutter/cupertino.dart';
import 'package:gold/generated/json/base/json_convert_content.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';

/// 描述: 配置数据
/// 
/// @Author tangzhentao
///  @CreatedTime 20:08 2022/6/17
///

/// 页面状态
/// [loading] 正在加载
/// [success] 成功
/// [error] 错误
/// [empty] 空数据
enum PageState { loading, success, error, empty }

/// 黄金回收类型 online=线上,offline=线下
class RecoverType {
  /// 线上
  static const online = 'online';
  /// 线下
  static const offline = 'offline';
}

/// 黄金回收取消类型 取消类型:0=用户取消,1=平台取消
class RecoverCancelType {
  /// 用户取消
  static const personal = '0';
  /// 平台取消
  static const platform = '1';
}

/// 黄金回收订单状态：空字符串='全部','-1'=已取消,'1'=待取件,'2'=待收件,'3'=待核件,'4'=待确认,'5'=已完成
class RecoverOrderStatus {
  /// 全部 空字符串
  static const all = '';
  /// 待取件 1
  static const unPickUp = '1';
  /// 待收件 2
  static const unReceipt = '2';
  /// 待核件 3
  static const unVerify = '3';
  /// 待确认 4
  static const unConfirm = '4';
  /// 已完成 5
  static const done = '5';
  /// 已取消 -1
  static const canceled = '-1';
}

/// 类型 product=购物,gold=黄金,points=积分
class ProductType {
  /// 购物
  static const product = 'product';
  /// 黄金
  static const gold = 'gold';
  /// 积分
  static const points = 'points';
}

/// 订单状态：-1=已取消,0=待支付,1=待发货,2=待收货（待提货）,3=待评价,4=已完成,5=已授权
class OrderStatus {
  /// 全部 ''
  static const all = '';
  /// 待支付 0
  static const paying = '0';
  /// 待发货 1
  static const sending = '1';
  /// 待收货/待提货 2
  static const receiving = '2';
  /// 待评价 3
  static const reviewing = '3';
  /// 已完成 4
  static const done = '4';
  /// 已授权 5
  static const authorized = '5';
  /// 已取消 -1
  static const cancel = '-1';
}

/// 退款状态:0=未申请,1=申请中,2=通过退款,3=拒绝退款
class RefundStatus {
  /// 未申请 0
  static const unApply = 0;
  /// 申请中 1
  static const applying = 1;
  /// 通过退款 2
  static const refunded = 2;
  /// 拒绝退款 3
  static const refused = 3;
}

/// 用户角色，0：普通用户、1：推广员、2：店主
class UserRole {
  /// 普通用户
  static const ordinary = 0;
  /// 推广员
  static const promoter = 1;
  /// 店主
  static const shopOwner = 2;
}

/// 系统配置项
/// 黄金回收保证金比例（%）→ collateral_rate
/// 平台服务协议 → service_agreement
/// 我的店铺规则 → store_rules
/// 我要开店规则 → open_store_rules
/// 积分规则说明 → points_rules
/// 实名认证协议 → real_name_authentication
/// 购买黄金制品协议 → gold_purchase_agreement
/// 黄金回收订单取消订单原因 → gold_recycle_cancel_reason
class SystemConfig {
  /// 黄金回收保证金比例
  static const collateralRate = 'collateral_rate';
  /// 平台服务协议
  static const serviceAgreement = 'service_agreement';
  /// 我的店铺规则
  static const storeRules = 'store_rules';
  /// 我要开店规则
  static const openStoreRules = 'open_store_rules';
  /// 积分规则说明
  static const pointsRules = 'points_rules';
  /// 实名认证协议
  static const realNameAuthentication = 'real_name_authentication';
  /// 购买黄金制品协议
  static const goldPurchaseAgreement = 'gold_purchase_agreement';
}

/// 获取快递状态的描述
/// 快递状态 1 暂无记录 2 在途中 3 派送中 4 已签收 (完结状态) 5 用户拒签 6 疑难件 7 无效单(完结状态) 8 超时单 9 签收失败 10 退回
String getExpressStatusDesc(int? status) {
  switch(status) {
    case 2:
      return '在途中';
    case 3:
      return '派送中';
    case 4:
      return '已签收';
    case 5:
      return '拒签';
    case 6:
      return '疑难件';
    case 7:
      return '无效单';
    case 8:
      return '超时单';
    case 9:
      return '签收失败';
    case 10:
      return '退回';
    default:
      return '暂无记录';
  }
}

/// 页面异常状态信息
class StateInfo {
  /// 提示信息
  String msg;
  /// 图片信息
  String image;

  StateInfo(this.msg, this.image);
}

/// 正在加载页面
Widget loadingWidget({Color? backgroundColor}) => Container(
  color: backgroundColor?? XfgColor.white,
  alignment: Alignment.center,
  child: Image.asset(Res.imagesPageLoad, width: XfgSize().size136, height: XfgSize().size120),
);

/// 空数据页面
/// [empty] 空数据信息
Widget emptyWidget(StateInfo empty, Color backgroundColor, {Image? image, GestureTapCallback? onClick}) => Container(
  color: backgroundColor,
  alignment: Alignment.center,
  child: GestureDetector(
    onTap: onClick,
    behavior: HitTestBehavior.opaque,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Image.asset(empty.image, width: XfgSize().size133, fit: BoxFit.fitWidth),
        SizedBox(height: XfgSize().size28),
        Text(empty.msg, style: TextStyle(color: XfgColor.text2, fontSize: XfgSize().text14), textAlign: TextAlign.center),
      ],
    ),
  ),
);

/// 错误页面
/// [failure] 错误信息
/// [reLoad] 重新加载
Widget errorWidget(StateInfo failure, Color backgroundColor, {GestureTapCallback? onReLoad}) => Container(
  alignment: Alignment.center,
  color: backgroundColor,
  child: GestureDetector(
    onTap: onReLoad,
    behavior: HitTestBehavior.opaque,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Image.asset(failure.image, width: XfgSize().size133, fit: BoxFit.fitWidth),
        SizedBox(height: XfgSize().size28),
        Text(failure.msg, style: TextStyle(color: XfgColor.text2, fontSize: XfgSize().text14), textAlign: TextAlign.center),
      ],
    ),
  ),
);

/// 全网比价数据
class PeerModel {
  String? logo;
  String? name;
  double? price;

  PeerModel();

  factory PeerModel.fromJson(Map<String, dynamic> json) {
    final PeerModel peerModel = PeerModel();
    peerModel.logo = jsonConvert.convert<String>(json['logo']);
    peerModel.name = jsonConvert.convert<String>(json['name']);
    peerModel.price = jsonConvert.convert<double>(json['price']);
    return peerModel;
  }
}

/// 请求创建订单信息
class OrderRequest {
  /// 应付金额
  double amount;
  /// 收货地址ID
  int? addressId;
  /// 产品ID
  int? productId;
  /// 数量
  int quantity;
  /// 积分抵扣数量
  int pointsNum;
  /// 余额抵扣数量
  double amountNum;
  /// 用户优惠券ID
  int? userCouponId;

  OrderRequest(
      this.amount,
      this.addressId, this.productId, this.quantity,
      this.pointsNum, this.amountNum, this.userCouponId,);

  Map<String, dynamic> toJson() {
    final map = {
      'address_id': addressId?.toString(),
      'product_id': productId.toString(),
      'quantity': quantity.toString(),
      'points_num': pointsNum.toString(),
      'amount_num': '0',
    };
    if (userCouponId != null) map['user_coupon_id'] = userCouponId.toString();
    return map;
  }
}